package com.hyt.it.ogt.kq.common.gov.utils;


import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.hutool.core.io.file.FileReader;


/**
 *
* @author: wangrl
* @date: 2021-09-04
*
 */
public class TextUtil {


	/**
	 * 判断一个对象所有值是否都为空
	 * @param object
	 * @return
	 */
	public static boolean checkObjAllFieldsIsNull(Object object) {
		if (null == object) {
			return true;
		}
		try {
			for (Field f : object.getClass().getDeclaredFields()) {
				f.setAccessible(true);
				if (f.get(object) != null && isNotNull(f.get(object).toString())) {
					return false;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}




	/**
	 * 从字符串中提取数字
	 * @param str
	 * @return
	 */
	public static Integer strExtractIntegerNum(String str){
		Integer num = null;
		String regEx="[^0-9]";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(str);
		String numStr =  m.replaceAll("").trim();
		if(TextUtil.isNum(numStr)){
			num = Integer.valueOf(numStr);
		}
		return num;
	}

	/**
	 * 
	 * @Title: isInteger   
	 * @Description: (判断是否为整数)   
	 * @param: @param str
	 * @param: @return      
	 * @return: boolean   
	 * @Author: wangruolan@tanqiu.com   
	 * @date:   2020年8月4日 上午11:28:31  
	 * @throws
	 */
	public static boolean isInteger(String str) {
		try {
			int i = Integer.parseInt(str);
			if(i < 0){
				return false;
			}
			return true;
		} catch (NumberFormatException var2) {
			return false;
		}
	}

	/**
	 * 判断是否为一个有效的Ip地址
	 * @param str
	 * @return
	 */
	public static boolean isIpAddress(String str){
		boolean result = false;
		final String regex =" " ,replacement = "",matchRegex = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
		final int length = 0;
		if (str != null && str.replaceAll(regex, replacement).length() > length &&
			str.matches(matchRegex)) {
			result = true;
		}
		return result;
		
		
	}
	

	/**
	 * 验证号码有效性
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isPhoneNum(String str) {
		boolean result = false;
		final String target = " ",replacement ="",matchRegex = "^[1][0-9][0-9]{9}$";
		if (str != null && str.replace(target, replacement).length() > 0 && str.matches(matchRegex)) {
			result = true;
		}
		return result;
	}

	public static boolean isNum(String str) {
		boolean result = false;
		final String target = " ",replacement = "",regex = "^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$";
		final int length = 10;
		if (str != null && str.replace(target, replacement).length() > 0 && str.length() < length
				&& str.matches(regex)) {
			result = true;
		}
		return result;
	}

	public static boolean isIntegerNum(String str) {
		boolean result = false;
		final String target = " ",replacement = "",regex = "^[-+]?(([0-9]+)(([0-9]+))?|(([0-9]+))?)$";
		final int length = 10;
		if (str != null && str.replace(target, replacement).length() > 0 && str.length() < length
				&& str.matches(regex)) {
			result = true;
		}
		return result;
	}
	
	
	public static String processChinese(String chinese) {

		try {
			final String charsetNameGbk = "GBK",charsetNameIso88591 = "ISO8859_1";
			byte[] byteArray = chinese.getBytes(charsetNameGbk);
			chinese = new String(byteArray, charsetNameIso88591);
		} catch (Exception e) {

		}
		return chinese;
	}

	public static String processChineseOther(String chinese) {

		try {
			final String charsetNameGbk2312 = "GB2312",charsetNameIso88591 = "ISO-8859-1";
			byte[] byteArray = chinese.getBytes(charsetNameIso88591);
			chinese = new String(byteArray, charsetNameGbk2312);
		} catch (Exception e) {

		}
		return chinese;
	}



	/**
	 * 转换中文为ISO表准编码
	 */
	public static String processIso(String chinese) {

		try {
			final String  gB2312 = "GB2312",iso88591 = "ISO-8859-1";
			byte[] byteArray = chinese.getBytes(gB2312);
			chinese = new String(byteArray, iso88591);
		} catch (Exception e) {

		}
		return chinese;
	}

	/**
	 * 判断字符串是否不为空
	 * 
	 * @param sstring
	 *            判断的字符串
	 * @return boolean :返回值为boolean
	 */
	public static boolean isNotNull(final String sstring) {
		boolean bolret = true;
		if (isNull(sstring)) {
			bolret = false;
		}
		return bolret;
	}

	/**
	 * 判断数字是否不为０
	 * 
	 * @param iint
	 *            判断的字符串
	 * @return boolean :返回值为boolean
	 */
	public static boolean isNotNull(int iint) {
		boolean bolret = true;
		if (iint <= 0) {
			bolret = false;
		}
		return bolret;
	}

	/**
	 * 判断浮点数>0
	 * 
	 * @param iint
	 *            判断的字符
	 * @return boolean :返回值为boolean
	 */
	public static boolean isNotNull(float iint) {
		boolean bolret = true;
		if (iint <= 0) {
			bolret = false;
		}
		return bolret;
	}

	/**
	 * 判断数字是否不为０
	 * 
	 * @param iint
	 *            判断的字符串
	 * @return boolean :返回值为boolean
	 */
	public static boolean isNotNull(Integer iint) {
		boolean bolret = true;
		if (iint == null) {
			bolret = false;
		}
		return bolret;
	}

	/**
	 * 判断数字是否不为０
	 * 
	 * @param iint
	 *            判断的字符串
	 * @return boolean :返回值为boolean
	 */
	public static boolean isNull(Integer iint) {
		boolean ret = false;
		if (iint == null) {
			ret = true;
		}

		return ret;
	}

	/**
	 * 判断对象是否为null
	 * 
	 * @param obj
	 *            判断的字符串
	 * @return boolean :返回值为boolean
	 */

	public static boolean isNull(Object obj) {
		if (obj == null) {
			return true;
		}
		final String regex = "{}";
		if(regex.equals(obj.toString())) {
			return true;
		}

		return false;
	}
	
	/**
	 * 判断对象是否为null
	 * 
	 * @param obj
	 *            判断的字符串
	 * @return boolean :返回值为boolean
	 */
	public static boolean isNotNull(Object obj) {
		return !isNull(obj);
	}
	
	

	/**
	 * 判断字符串是否不为空
	 * 
	 * @param sstring
	 *            判断的字符串
	 * @return boolean :返回值为boolean
	 */
	public static boolean isNull(final String sstring) {
		boolean bolret = false;
		final String oull ="null",empty ="";
		if (sstring == null || oull.equals(sstring.trim()) || empty.equals(sstring.trim())) {
			bolret = true;
		}
		return bolret;
	}

	/**
	 * 判断列表是否为空
	 *
	 *            判断的字符串
	 * @return boolean :返回值为boolean
	 */
	public static boolean isNull(List lst) {
		boolean bolret = false;
		if (lst == null) {
			bolret = true;
		}
		if (lst != null) {
			if (lst.size() == 0) {
				bolret = true;
			}
		}
		return bolret;
	}

	public static boolean isNotNull(List lst) {
		boolean bolret = false;
		if (lst != null) {
			if (lst.size() > 0) {
				bolret = true;
			}
		}
		return bolret;
	}

	/**
	 * 判断数字是否不为０
	 * 
	 * @param iint
	 *            判断的字符串
	 * @return boolean :返回值为boolean
	 */
	public static boolean isNull(int iint) {
		boolean bolret = false;
		if (iint == 0) {
			bolret = true;
		}
		return bolret;
	}


	/**
	 * 判断字符串是否为空,并赋上默认值
	 * 
	 * @param sstring
	 *            判断的字符串
	 * @param sdefault
	 *            默认值
	 * @return String :返回值为String
	 */
	public static String replaceNullWith(String sstring, String sdefault) {
		String sret = null;
		if (isNotNull(sstring)) {
			sret = sstring;
		} else {
			sret = sdefault;
		}

		return sret;
	}






	/** 字符串URLEncoder编码  
	 * @param str 待编码字符串
	 * @param charset 编码格式 */
	public static String encoderUrl(String str, String charset) throws UnsupportedEncodingException {
		final String empty = "",code ="utf-8";
		if (str == null) {
			return empty;
		}
		if (charset != null && !empty.equals(charset)) {
			try {
				return URLEncoder.encode(str, charset);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				return null;
			}
		}
		return URLEncoder.encode(str,code);

	}
	
	/** 字符串URLDecode解码  
	 * @param str 待解码字符串
	 * @param charset 解码格式 */
	public static String decoderUrl(String str, String charset) throws UnsupportedEncodingException {
		final String empty = "",code ="utf-8";
		if (str == null) {
			return empty;
		}
		if (charset != null && !empty.equals(charset)) {
			try {
				return URLDecoder.decode(str, charset);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				return null;
			}
		}
		return URLDecoder.decode(str,code);

	}



	/**
	 * 判断是否为布尔值
	 * @param str
	 * @return
	 */
	public static boolean isBoolean(String str) {
		boolean flag =false;
		final String isTrue ="true", isFalse ="false", isOne = "1",isZero = "0";
		if(isTrue.equals(str)||isFalse.equals(str)||isOne.equals(str)||isZero.equals(str)){
			return true;
		}
		return flag;
	}

	/**
	 * 判断是否不为布尔值
	 * @param str
	 * @return
	 */
	public static boolean isNotBoolean(String str){
		return !isBoolean(str);
	}



	/**
	 * 判断是否为long类型
	 * @param str
	 * @return
	 */
	public static boolean isLongNum(String str) {
		try {
			long l = Long.parseLong(str);
			if(l < 0){
				return false;
			}
			return true;
		} catch (NumberFormatException var2) {
			return false;
		}
	}


	/**
	 * 
	 * @Title: isDate   
	 * @Description: (判断是否为有效日期)   
	 * @param: @param str
	 * @param: @return      
	 * @return: boolean
	 * @throws
	 */
	public static boolean isDate(String str) {
		 if(isNull(str)) {
			 return false;
		 }
		 try {
			 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			 dateFormat.parse(str);
		 }catch (ParseException e){
		 	return false;
		 }

	 	return true;
	}


	
	/**
	 * 
	 * @Title: isNotDate   
	 * @Description: (判断不是有效日期)   
	 * @param: @param str
	 * @param: @return      
	 * @return: boolean
	 * @throws
	 */
	public static boolean isNotDate(String str){
		return !isDate(str);
	}


	/**
	 * xml去除非法字符串转utf-8
	 * @param xml
	 * @return
	 */
	public static String getUtf8XmlString(String xml) {
		StringBuffer sb = new StringBuffer();
		sb.append(xml);
		String xmString = "";
		String xmlUtf8="";
		try {
			xmString = new String(sb.toString().getBytes("UTF-8"));
			xmlUtf8 = URLEncoder.encode(xmString, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return xmlUtf8;
	}

	/**
	 * 去除空格与换行
	 * @param str
	 * @return
	 */
	public static String stringTrm(String str){
		if(isNotNull(str)){
			return str.replaceAll("\r\n|\r|\n", "");
		}
		return null;
	}

	/**
	 * ResponseCode.java类中的code码，打出在控制台。
	 * @param path
	 */
	public static void printResponseCode(String path) {
		FileReader fileReader = new FileReader(path);
		fileReader.readLines().stream().forEach(str -> {
			if (str.contains(":") && !str.contains(".")) {
				System.out.println(str.replaceAll("\\*","")
						.replaceAll(" ","")
						.replaceAll(":",": "));
			}
		});

	}

	public static void main(String[] args) {
		String path = "C:\\Users\\hyt820pc\\Desktop\\考生\\ResponseCode.java";



		printResponseCode(path);
	}
	
}
